package com.cdn.nodes.controller;

import java.util.List;

import com.cdn.nodes.domain.EdgeIpList;
import com.cdn.nodes.domain.dto.EdgeIpListAddDTO;
import com.cdn.nodes.service.IEdgeIpListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cdn.common.log.annotation.Log;
import com.cdn.common.log.enums.BusinessType;
import com.cdn.common.security.annotation.RequiresPermissions;
import com.cdn.common.core.web.controller.BaseController;
import com.cdn.common.core.web.domain.AjaxResult;
import com.cdn.common.core.web.page.TableDataInfo;

/**
 * IP名单，记录允许或禁止访问网站的IP地址Controller
 * 
 * @author Cinzia
 * @date 2024-12-20
 */
@RestController
@RequestMapping("/ip")
public class EdgeIpListController extends BaseController
{
    @Autowired
    private IEdgeIpListService edgeIpListService;

    /**
     * 查询IP名单，记录允许或禁止访问网站的IP地址列表
     */
    @RequiresPermissions("node:ip:list")
    @GetMapping("/list")
    public TableDataInfo list(EdgeIpList edgeIpList)
    {
        startPage();
        List<EdgeIpList> list = edgeIpListService.selectEdgeIpListList(edgeIpList);
        return getDataTable(list);
    }
//
//    /**
//     * 导出IP名单，记录允许或禁止访问网站的IP地址列表
//     */
//    @RequiresPermissions("system:list:export")
//    @Log(title = "IP名单，记录允许或禁止访问网站的IP地址", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, EdgeIpList edgeIpList)
//    {
//        List<EdgeIpList> list = edgeIpListService.selectEdgeIpListList(edgeIpList);
//        ExcelUtil<EdgeIpList> util = new ExcelUtil<EdgeIpList>(EdgeIpList.class);
//        util.exportExcel(response, list, "IP名单，记录允许或禁止访问网站的IP地址数据");
//    }

    /**
     * 获取IP名单，记录允许或禁止访问网站的IP地址详细信息
     */
    @RequiresPermissions("node:ip:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(edgeIpListService.selectEdgeIpListById(id));
    }

    /**
     * 新增IP名单，记录允许或禁止访问网站的IP地址
     */
    @RequiresPermissions("node:ip:add")
    @Log(title = "IP名单，记录允许或禁止访问网站的IP地址", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EdgeIpListAddDTO edgeIpListAddDTO)
    {
        return toAjax(edgeIpListService.insertEdgeIpList(edgeIpListAddDTO));
    }

    /**
     * 修改IP名单，记录允许或禁止访问网站的IP地址
     */
    @RequiresPermissions("node:ip:edit")
    @Log(title = "IP名单，记录允许或禁止访问网站的IP地址", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EdgeIpList edgeIpList)
    {
        return toAjax(edgeIpListService.updateEdgeIpList(edgeIpList));
    }

    /**
     * 删除IP名单，记录允许或禁止访问网站的IP地址
     */
    @RequiresPermissions("node:ip:remove")
    @Log(title = "IP名单，记录允许或禁止访问网站的IP地址", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(edgeIpListService.deleteEdgeIpListByIds(ids));
    }

    /**
     * 根据节点Id查询ip地址
     */
    @GetMapping("/getIpIdByNodeId/{nodeId}")
    public AjaxResult getIpIdByNodeId(@PathVariable Long nodeId){
        return success(edgeIpListService.getIpIdByNodeId(nodeId));
    }

}
